package com.sea.product.service.impl;


import com.codecrane.core.power.entity.DictDetail;
import com.codecrane.core.power.service.DictDetailService;
import com.codecrane.core.web.BatchImportUtils;
import com.sea.product.entity.Category;
import com.sea.product.entity.Channel;
import com.sea.product.entity.Product;
import com.sea.product.mapper.CategoryMapper;
import com.sea.product.mapper.ProductMapper;
import com.sea.product.service.CategoryService;
import com.sea.product.service.ProductService;
import com.sea.supply.entity.ProductPrice;
import com.sea.supply.entity.Supplier;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.List;

/**
 * @author 陈鸿飞
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private DictDetailService dictDetailService;
    @Autowired
    private CategoryService categoryService;
    @Override
    public List<Product> findByCnd(Product product) {
        return productMapper.findByCnd(product);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int save(Product product) {
        System.out.println(product.getProductSku());
        product.setProductOutBoxVolume((product.getProductOutBoxLength()*product.getProductOutBoxWidth()*product.getProductOutBoxHeight())/1000000);
        product.setProductVolume((product.getProductLength()*product.getProductWidth()*product.getProductHeight())/1000000);
        String productUpc = product.getProductUpc();
        if(StringUtils.isEmpty(productUpc)){
            throw new RuntimeException("UPC不能为空!");
        }else{
            checkUpc(productUpc);
        }
        return productMapper.insert(product);
    }

    @Override
    public Product findById(Integer id) {
        return productMapper.findById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int usave(Product product) {
        product.setProductOutBoxVolume((product.getProductOutBoxLength()*product.getProductOutBoxWidth()*product.getProductOutBoxHeight())/1000000);
        product.setProductVolume((product.getProductLength()*product.getProductWidth()*product.getProductHeight())/1000000);
        String productUpc = product.getProductUpc();
        if(StringUtils.isEmpty(productUpc)){
            throw new RuntimeException("UPC不能为空!");
        }else{
            checkUpc(productUpc);
        }
        return productMapper.update(product);
    }

    @Override
    public int delete(Integer id) {
        return productMapper.delete(id);
    }

    @Override
    public Product findBySku(String productSku) {
        return productMapper.findBySku(productSku);
    }

    @Override
    public List<Product> getProduce(Product product) {
        return productMapper.getProduce(product);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void importSku(InputStream inputStream, String fileName) throws Exception {
        Workbook workBook = BatchImportUtils.getWorkBook(inputStream, fileName);
        List<Sheet> sheets = BatchImportUtils.getSheets(workBook);
        if (null != sheets && sheets.size() > 0) {
            Sheet sheet = sheets.get(0);
            List<Product> list = BatchImportUtils.iteratorForProduct(sheet);
            if (null == list || list.size() == 0) {
                throw new Exception("请至少保存一条SKU信息！");
            } else {
                for (Product product : list) {
                    Product sku = productMapper.findBySku(product.getProductSku());
                    String propertyId = dictDetailService.findByTypeName(23, product.getProductPropertyName());
                    if (null == propertyId) {
                        throw new Exception("未查询到该属性:" + product.getProductPropertyName());
                    } else {
                        product.setProductPropertyId(propertyId);
                    }
                    String colorId = dictDetailService.findByTypeName(22, product.getProductColorName());
                    if (null == colorId) {
                        throw new Exception("未查询到该颜色:" + product.getProductColorName());
                    } else {
                        product.setProductColorId(colorId);
                    }
                    String sizeId = dictDetailService.findByTypeName(24, product.getProductSizeName());
                    if (null == sizeId) {
                        throw new Exception("未查询到该尺码:" + product.getProductSizeName());
                    } else {
                        product.setProductSizeId(sizeId);
                    }
                    //查询SKU
                    Integer bigId = categoryService.findIdByName(product.getProductBigClassName());
                    if (null == bigId) {
                        throw new Exception("未查询到该大类:" + product.getProductBigClassName());
                    } else {
                        product.setProductBigClassId(bigId);
                    }
                    Integer midId = categoryService.findIdByName(product.getProductMidClassName());
                    if (null == midId) {
                        throw new Exception("未查询到该中类:" + product.getProductMidClassName());
                    } else {
                        product.setProductMidClassId(midId);
                    }
                    Integer classId = categoryService.findIdByName(product.getProductClassName());
                    if (null == classId) {
                        throw new Exception("未查询到该小类:" + product.getProductClassName());
                    } else {
                        product.setProductClassId(classId);
                    }
                    if(null!=sku) {
                        product.setId(sku.getId());
                        usave(product);
                    }else{
                        throw new Exception("暂未查询到该SKU:" + product.getProductSku());
                    }
                }
            }
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void importAddSku(InputStream inputStream, String fileName) throws Exception {
        Workbook workBook = BatchImportUtils.getWorkBook(inputStream, fileName);
        List<Sheet> sheets = BatchImportUtils.getSheets(workBook);
        if (null != sheets && sheets.size() > 0) {
            Sheet sheet = sheets.get(0);
            List<Product> list = BatchImportUtils.iteratorForProduct(sheet);
            if (null == list || list.size() == 0) {
                throw new Exception("请至少保存一条SKU信息！");
            } else {
                for (Product product : list) {
                    Product sku = productMapper.findBySku(product.getProductSku());
                    String propertyId = dictDetailService.findByTypeName(23, product.getProductPropertyName());
                    if (null == propertyId) {
                        throw new Exception("未查询到该属性:" + product.getProductPropertyName());
                    } else {
                        product.setProductPropertyId(propertyId);
                    }
                    String colorId = dictDetailService.findByTypeName(22, product.getProductColorName());
                    if (null == colorId) {
                        throw new Exception("未查询到该颜色:" + product.getProductColorName());
                    } else {
                        product.setProductColorId(colorId);
                    }
                    String sizeId = dictDetailService.findByTypeName(24, product.getProductSizeName());
                    if (null == sizeId) {
                        throw new Exception("未查询到该尺码:" + product.getProductSizeName());
                    } else {
                        product.setProductSizeId(sizeId);
                    }
                    //查询SKU
                    Integer bigId = categoryService.findIdByName(product.getProductBigClassName());
                    if (null == bigId) {
                        throw new Exception("未查询到该大类:" + product.getProductBigClassName());
                    } else {
                        product.setProductBigClassId(bigId);
                    }
                    Integer midId = categoryService.findIdByName(product.getProductMidClassName());
                    if (null == midId) {
                        throw new Exception("未查询到该中类:" + product.getProductMidClassName());
                    } else {
                        product.setProductMidClassId(midId);
                    }
                    Integer classId = categoryService.findIdByName(product.getProductClassName());
                    if (null == classId) {
                        throw new Exception("未查询到该小类:" + product.getProductClassName());
                    } else {
                        product.setProductClassId(classId);
                    }
                    if(null==sku) {
                        save(product);
                    }else{
                        throw new RuntimeException("该SKU已经存在,请勿重复提交:" + product.getProductSku());
                    }
                }
            }
        }
    }

    void checkUpc(String productUpc){
        List<DictDetail> upcList = dictDetailService.findByTypeCode("base_upc", null);
        Boolean flag=false;
        for(DictDetail dictDetail:upcList){
            String detailName = dictDetail.getDetailName();
            if(productUpc.startsWith(detailName)){
                flag=true;
                break;
            }
        }
        if(flag.equals(false)){
            throw new RuntimeException("请填写正确的UPC");
        }
    }
}
